function smilesGUI(adjust)
% This is the GUI for SiMSiuaD. Development of this function started with a
% static 800x800 GUI figure, but ever since release rev37 the figure
% auto-adjusts based on the variable adjust (which is calculated in
% function checkScreen, and is based on the current screenresolution).
% adjust should only range from 0.6 to 2.0, depending on screenresolution.

%%  Initialization tasks & Construct components
sfig = figure('MenuBar','none','Name','SMILES','NumberTitle',...
    'off','Position',[50,50,800*adjust(1),800*adjust(2)],'Resize','off'); %Creates GUI window

AX = axes('Parent',sfig,'Position',[.00 .05 1.0 0.95]);
set(AX,'Box','on','Xlim',[0 100],'YLim',[0 100],'XTick',[],'YTick',[]) %Creates an area for figures
whitebg('black') %and white just became black!

% TogD = uicontrol(sfig,'Style','checkbox',...
%     'String','Toggle 3D on/off','Value',0,'Position',[290 160 100 15],...
%     'Callback',@ToggleDim); %Adds a checkbox to GUI
%
inputstr = sprintf('Enter SMILES formula here.'); %Sets initial text
inSmiles = uicontrol(sfig,'Style','edit',...
    'String',inputstr,'Max',1,'Min',0,...
    'Position',[150*adjust(1) 01*adjust(2) 530*adjust(1) 39*adjust(2)]); %[begix x-as, begin y-as, lengte, hoogte]
%creates a textbox in which SMILES may be inserted

loader = uicontrol(sfig,'Style','pushbutton',...
    'String','Convert to structural formula','Position',[01*adjust(1) 01*adjust(2) 150*adjust(1) 40*adjust(2)],...
    'Callback',@Convert); %Adds a pushbutton

loadFile = uicontrol(sfig,'Style','pushbutton',...
    'String','Load','Position',[740*adjust(1),21*adjust(2),60*adjust(1),20*adjust(2)],...
    'Callback',@loadfile); %Adds a pushbutton to load savefiles

saveFile = uicontrol(sfig,'Style','pushbutton',...
    'String','Save','Position',[680*adjust(1),21*adjust(2),60*adjust(1),20*adjust(2)],...
    'Callback',@savefile); %Adds a pushbutton to save savefiles

about = uicontrol('Style','PushButton','String','About','Position',[680*adjust(1),01*adjust(2),60*adjust(1),20*adjust(2)],...
    'CallBack',@AboutBox); %Adds a pushbutton to show the about window

uicontrol('Style','PushButton','String','Close','Position',[740*adjust(1),01*adjust(2),60*adjust(1),20*adjust(2)],...
    'CallBack',@Close); %Adds a closebutton

set(gcf,'CloseRequestFcn',@Close); % intercepts the close function when
% closing the GUI using the upper right closebutton. It passes on the
% CallBack to our own Close function.

%%  Callbacks for smilesGUI
    function Convert(varargin)
        % This function is used to get the string entered by the user, and
        % converts that string to a 2D-structure by passing it on to
        % SMILES.
        input = get(inSmiles,'String'); %Gets input from GUI
        if strcmp(inputstr,input) ~= 1
            cla % Clears axes
            [rings main] = SMILES(input,1); %passes on input to CORE of application, and passes along a GUI tag.
            mm = [min(real([main.x])) max(real([main.x]));...
                min(real([main.y])) max(real([main.y]))]; % calculates
            % the limits for both axes x and y, which depends on the
            % smallest and largest numbers of x and y.
            if mm(1,1) < mm(2,1)
                a = 1;
            else
                a = 2;
            end
            if mm(1,2) > mm(2,2)
                b = 1;
            else
                b = 2;
            end % lowest and highest coordinates are used to determine the 
            % range of the axes, and sets the range
            set(AX,'XLim',[mm(a,1)-1 mm(b,2)+1],'YLim',...
                [mm(a,1)-1 mm(b,2)+1]);
            drawnow
        else
            disp('Please enter a SMILES formula')
        end
    end

%% About Msgbox
    function AboutBox(varargin)
        % This function opens the about window, which shows some basic
        % information. This function is ran when the
        % pushbutton about has been pressed.
        AboutText = sprintf(['This application was created by Peter Betlem en Freek Hoogstede.'...
            '\nUpdates can be found at \n http://code.google.com/p/matlab-project-smiles/'...
           'or by running Updater.m']);
        AboutTitle = sprintf('About SMILES');
        msgbox(AboutText,AboutTitle,'help') %shows a messagebox with 
        % (with a help-icon); title = AboutTitle and text = AboutText
    end


%% Saving and loading structural formula's:
    function savefile(varargin)
        % This function passes on both the input and the current
        % axes-handle to the saving function, together with what the 
        % savefile will be named. This function is ran when the
        % pushbutton saveFile has been pressed.
        saveString = sprintf('Please enter the name to be used for the savefile...');
        name = inputdlg(saveString,'Saving...'); %asks user for the filename
        % under which the savefile will be created.
        if ~isempty(name) && ~isempty(name{1}) && isempty(find(name{1} == ' '))
            saveData(AX,sprintf('%s',get(inSmiles,'String')),name); %passes on data
            %to the saveData function, which will save input and axes in an extensionless file.
            % This only happens when name is a valid entry.
        elseif ~isempty(name)
            msgbox('Please enter a valid name') % In case nothing has been
            % entered
        end
    end

    function loadfile(varargin)
        % This function loads both the input and restores the loaded
        % axes-data to the axes from an extensionless savefile.
        % This function is ran when the
        % pushbutton saveFile has been pressed. The user is then asked
        % which name the to-be-loaded savefile has been given.
        loadString = sprintf('Please enter the name of the file you wish to load...');
        name = inputdlg(loadString,'Loading...'); %asks for userinput
        if ~isempty(name) && isempty(find(name{1} == ' ')) && ~isempty(name{1})
            % This only happens when name is a valid entry
            % for our loading system
            if fileExists(name{1},'.\SaveFiles','decode') == 1
                % and the extensionless file was found by fileExists (and
                % thus was unzipped)
                inpoot= getData(name); %retrieves inputdata from savefile
                set(inSmiles,'String',inpoot{1}) %edits inputbox by setting the loaded SMILES-line
            fileExists(name{1},'.\SaveFiles','encode'); 
            % after loading the savefile, the savefile is once more encoded
            % to avoid being corrupted by the user, and to be able to load
            % the same savefile in future, the extension of the savefile is
            % removed.
            end
        elseif ~isempty(name)
            msgbox('Please enter a valid name')
            % In case nothing has been etnered by the user.
        end
    end

%% Closing
    function Close(varargin)
        % This function closes the SiMSiuaD application in the right
        % manner in several steps. First it deletes the GUI figure, after
        % which all paths are removed from the MATLAB searchpath, and a
        % thanks message is displayed.
        delete(gcbf)
        loadPaths('unload')
        rmpath('.\Core\Drivers')
        disp('Thank you for using SMILES in Matlab: Stepping it up a dimension')
    end
end